<?php
session_start();
require_once("../config/include_tokyo.inc.php");
require_once("../class/Common_Function.php");
require_once("../class/User.php");
require_once("../class/Core_Lib_Page.php"); //翻页

//访问权限验证
$userRole = $_SESSION['user_role'];
if($userRole == 1 ){
	//管理员 
}else{
	if($userRole > 0){
		$url = "../index.php?loginError=2";//没权限
	}else{
		$url = "../index.php?loginError=3";//没登录
	}	
	echo "<script language='javascript' type='text/javascript'>";
	echo "window.location.href='$url'";
	echo "</script>";
}

//set default
$userId = 0;//0 is default, show all users;

//get post value
$doFlag = $_POST["doFlag"];
$id = $_POST["id"];


//do actions
if($doFlag == "doDelete"){
	//删除 今日工作
	$sqlDel = "delete from teamdiary where id= $id";
	$db->Execute($sqlDel);
}else if($doFlag == "doSearch"){
	//搜索
	$userId = $_POST["userId"];
}



//获取所有用户列表
$sql = "SELECT * from users order by id asc"; 
$result = $db->Execute($sql);
$allUser = array();
$i=0;
while (!$result->EOF) {
	$tempUser = new User();
	$tempUser->id = $result->fields['id'];
	$tempUser->userName = $result->fields['user_name'];
	$tempUser->userRole = $result->fields['user_role'];
	$allUser[$i] = $tempUser;
	$i++;
	$result->MoveNext();
}
/*
foreach($allUser as $user){
	echo "id=".$user->id."  name=".$user->userName."<br>";
}
*/
 





//翻页
$listFlag = $_POST["listFlag"];
$list_rows = 20; //每页显示的记录数
if($userId == 0){
	$sql_all = "SELECT COUNT(*) as total_rows from teamdiary";
}else{
	$sql_all = "SELECT COUNT(*) as total_rows from teamdiary where user_id=$userId";
}
 

$result = $db->Execute($sql_all);
while (!$result->EOF) {
	$totalRows = $result->fields['total_rows'];
	$result->MoveNext();
}
$nowPage = $_POST['nowPage'];
$nowPage = !empty($nowPage) ? $nowPage : 1;
$params = array(
	'total_rows'=>$totalRows,
	'list_rows'=>$list_rows,
	'method'    =>'ajax',
	'ajax_func_name' =>'goToPage',
	'now_page' => $nowPage 
);
$page = new Core_Lib_Page($params);
$limit_m = $page->first_row;
$limit_n = $list_rows;



if($userId == 0){
	$sql_list = "select * from teamdiary order by id desc limit $limit_m,$limit_n";//default all
}else{
	$sql_list = "select * from teamdiary where user_id=$userId order by id desc limit $limit_m,$limit_n";
}



$result = $db->Execute($sql_list);
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<!--Begin:head-->
<? $pageTitle = "团队今日工作管理"; ?>
<?php include("../head.php"); ?>
<!--End:head-->


<body>
<?php include("../topMenu.php"); ?>


<?php include("teamMenu.php"); ?>


<form name="listForm" action="teamDiary.php" method="post">
<div id="container" >

<div id="searchDiary">
<div class="leftDiv">
<table boder=0><tr>
<td>
团队成员: 
<select name="userId" id="userId">
<option value=0 selected>全部</option>
<?php
$strOption = "";
foreach($allUser as $user){
	$strOption = $strOption."<option value=$user->id>$user->userName</option>";
}
echo $strOption;
?>
</select>
<br>
</td>
<td width=20>&nbsp;</td>
<td><a href="javascript:doSearch()" class="button icon search">搜索</a></td>
</tr></table>
</div>
</div>
<div class="clearBoth">&nbsp;</div>



<div class="grayTable">	
<table >
<thead>
	<tr>
	    <th scope="col" >日期</th>
		<th scope="col" >内容</th>
		<th scope="col" >成员</th>
		<th scope="col" >操作</th>
	</tr>
</thead>

<tbody>
	<?php
	$commonFunction = new Common_Function();
	$userFunc = new User();
	$today_date = date('Y-m-d');
	while (!$result->EOF) {
		$id = $result->fields['id'];
		$create_time = $result->fields['create_time'];
		$diary_content = $result->fields['diary_content'];
		$user_id = $result->fields['user_id'];
		$userName = $userFunc->getUserName($user_id,$allUser);
		//$diary_content = "<textarea  id='diary_content_$id' name='diary_content_$id' rows=5 cols=60>$diary_content</textarea>";
		$diary_content = "<div id='diary_content_$id'>$diary_content</div>";
		$createTime_date = substr($create_time,0,10);		
		$actionStr = "<a href='javascript:doDelete($id)' class='button icon remove'>删除</a>";		
		//-------------------------------------------------
		$strTR = "<tr >";
		$strTR = $strTR ."<td>".$create_time."</td>";
		$strTR = $strTR ."<td>".$diary_content."</td>";
		$strTR = $strTR ."<td>".$userName."</td>";
		$strTR = $strTR ."<td>".$actionStr."</td>";
		$strTR =  $strTR ."</tr >";
		$strTR =  $strTR ."<script> ";
		$strTR =  $strTR ."$('#diary_content_$id').html(replaceEnter2BR($('#diary_content_$id').html()));";
		$strTR =  $strTR ."</script> ";
		echo $strTR;
		$result->MoveNext();
	}
	?>
</tbody>

					
</table>

<br>

<!--Page 分页,翻页 -->
<div id="page"><?php echo $page->show(2);?></div>

</div>



</div><!--end container -->

<input type="hidden" name="doFlag" id="doFlag" >
<input type="hidden" name="id" id="id" >
<input type="hidden" name="nowPage" id="nowPage" value="<?php echo $nowPage; ?>"><!--翻页-->
</form>





</body>




</html>

<script>
/**翻页**/
function goToPage(nowPage){
	document.getElementById('doFlag').value = 'toList';
	document.getElementById('nowPage').value = nowPage;
	document.listForm.submit();
}



/** 删除 **/
function doDelete(id){
	if(confirm("是否删除这条记录,删除后不可恢复,是否继续?")){ 
		document.getElementById('doFlag').value = "doDelete";
		document.getElementById('id').value = id;
		document.listForm.submit();
	}else{
		return;
	}
}

/************搜索**************************/
function doSearch(){
	document.getElementById('doFlag').value = "doSearch";	
	document.listForm.submit();
}



</script>

